<?php defined('IN_IA') or exit('Access Denied');?><?php  include $this->template('common/header', TEMPLATE_INCLUDEPATH);?>
<?php  include $this->template('common', TEMPLATE_INCLUDEPATH);?>
<ul class="nav nav-tabs">
	<li <?php  if($operation == 'post') { ?>class="active"<?php  } ?>><a href="<?php  echo $this->createWebUrl('coupons', array('op' => 'post'));?>">添加优惠券</a></li>
	<li <?php  if($operation == 'display') { ?>class="active"<?php  } ?>><a href="<?php  echo $this->createWebUrl('coupons', array('op' => 'display'));?>">管理优惠券</a></li>
</li>
</ul>
<?php  if($operation == 'post') { ?>
<div class="main">
    <form action="" method="post" class="form-horizontal form" enctype="multipart/form-data" onsubmit="return formcheck();">
		<input type="hidden" name="id" value="<?php  echo $item['id'];?>" />
		<h4>添加优惠券</h4>
		<table class="tb">
			<!--<tr>-->
				<!--<th><label for="">排序</label></th>-->
				<!--<td>-->
					<!--<input type="text" name="displayorder" class="span2" value="<?php  echo $item['displayorder'];?>" />-->
				<!--</td>-->
			<!--</tr>-->
			<tr>
				<th><span class="red">*</span><label for="">优惠券类别</label></th>
				<td>
					<label class="radio inline"><input type="radio" checked="checked" name="coupon_type" value="0"/> 线下优惠券</label>
					&nbsp;&nbsp;&nbsp;
					<label class="radio inline"><input type="radio" name="coupon_type" value="1"/>派发优惠券</label>
                    &nbsp;&nbsp;&nbsp;
                    <label class="radio inline"><input type="radio" name="coupon_type" value="2"/>线上优惠券</label>
					<span class="help-block">此设置项设置优惠券类型。</span>
				</td>
			</tr>
			<tr>
				<th><span class="red">*</span><label for="">优惠券名称</label></th>
				<td>
					<input id="names" type="text" name="names" class="span5" value="<?php  echo $item['names'];?>" />
				</td>
			</tr>
            <tr>
                <th><span class="red">*</span><label for="">优惠券缩略图</label></th>
                <td>
                    <div class="fileupload fileupload-new" data-provides="fileupload">
                        <div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;">
                            <img src="./resource/attachment/<?php  echo $item['coupon_thumb'];?>" alt="" onerror="$(this).remove();"></div>
                        <div>
                            <span class="btn btn-file"  id="thumb_div" tabindex="-1"><span class="fileupload-new">选择图片</span>
                                <span class="fileupload-exists">更改</span><input name="coupon_thumb" id="thumb" type="file" /></span>
                            <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">移除</a>
                        </div>
                    </div>
                    <span class="help-block">此设置项设置优惠券缩略图</span>
                </td>
            </tr>
            <tr>
                <th><span class="red">*</span><label for="">优惠券面值</label></th>
                <td>
                    <input id="coupons_fee" type="text" name="coupon_fee" class="span1" value="<?php  echo $item['coupon_fee'];?>" /> 元
                </td>
            </tr>
			<tr>
				<th><span class="red">*</span><label for="">使用期限</label></th>
				<td>
					<?php  echo tpl_form_field_daterange('datelimit', array(), '')?>
				</td>
			</tr>
			<tr>
				<th><span class="red">*</span><label for="">日期限定</label></th>
				<td>
					<input type="checkbox" name="not_day[]" value="1"/>星期一
					<input type="checkbox" name="not_day[]" value="2"/>星期二
					<input type="checkbox" name="not_day[]" value="3"/>星期三
					<input type="checkbox" name="not_day[]" value="4"/>星期四
					<input type="checkbox" name="not_day[]" value="5"/>星期五
					<input type="checkbox" name="not_day[]" value="6"/>星期六
					<input type="checkbox" name="not_day[]" value="7"/>星期日
					<input name="chkAll" type="checkbox" id="chkAll" onClick=CheckAll(this.table) value="checkbox"/>全选
					<span class="help-block">此设置项设置限定日期使用此优惠券。</span>
				</td>
			</tr>
			<tr>
				<th><span class="red">*</span><label for="">每人可使用次数</label></th>
				<td>
					<input id="docount" type="text" name="docount" class="span2" value="<?php  echo $item['docount'];?>" />
					<span class="help-block">此设置项设置每个用户可使用此优惠券数量。</span>
				</td>
			</tr>
			<tr id="coupon_count">
				<th><span class="red">*</span><label for="">优惠券总张数</label></th>
				<td>
					<input id="count" type="text" name="count" class="span2" value="<?php  echo $item['count'];?>" /> 张
					<span class="help-block">此设置项设置优惠券的总发行数量。</span>
				</td>
			</tr>
            <tr id="ver_codes">
                <th><span class="red">*</span><label for="">优惠券兑换码</label></th>
                <td>
                    <input id="ver_code" type="text" name="ver_code" class="span2" value="<?php  echo $item['ver_code'];?>" /><input id="codes" type="button" class="btn  span2" value="重新生成" style="margin-left: 10px;"/>
                    <span class="help-block">此设置项设置优惠券的唯一兑换码。</span>
                </td>
            </tr>
            <tr>
                <th><span class="red">*</span><label for="">限定使用优惠券的最小金额</label></th>
                <td>
                    <input id="min_fee" type="text" name="min_fee" class="span1" value="<?php  echo $item['min_fee'];?>" /> 元
                    <span class="help-block">此设置项设置限定使用优惠券的最小金额。</span>
                </td>
            </tr>
			<tr>
				<th>优惠券描述</th>
				<td>
					<textarea style="height:400px; width:100%;" class="span7 richtext-clone" name="coupons_intro" cols="70"><?php  echo $item['coupons_intro'];?></textarea>
				</td>
			</tr>
		<tr>
			<th></th>
			<td>
				<input name="submitCoupon" type="submit" value="提交" class="btn btn-primary span3">
				<input type="hidden" name="token" value="<?php  echo $_W['token'];?>" />
			</td>
		</tr>
		</table>
	</form>
</div>
<script type="text/javascript">
<!--
	kindeditor($('.richtext-clone'));
//-->

 function CheckAll(){
            if ($("#chkAll").attr("checked")) {
                $(":checkbox").attr("checked", true);
            } else {
                $(":checkbox").attr("checked", false);
            }
        }  
//生成兑换码
$(document).ready(function(){
    $("#ver_code").val(createCode());
    $("#codes").on('click',function(){
        $("#ver_code").val(createCode());
    });
});
//优惠券类别
$(":radio").click(function() {
    if($(this).val() == "1"){
        $('#ver_codes').hide();
        $('#coupon_count').hide();
    }else if($(this).val() == "2"){
        $('#ver_codes').hide();
        $('#coupon_count').show();
    }else{
        $('#ver_codes').show();
        $('#coupon_count').show();
    }
});
function createCode()
{
    var code="";
    var codeLength=8;
    var selectChar=new Array(0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F","G","H","I","G","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
    for(var i=0;i<codeLength;i++)
    {
        var charIndex=Math.floor(Math.random()*32);
        code += selectChar[charIndex];
    }
    if(code.length != codeLength)
    {
        createCode();
    }
    return code;
}
//数据验证
function formcheck() {
    if ($("#names").val() == '') {
        Tip.focus("names", "请输入优惠券名称!", "right");
        return false;
    }
    if ($("#coupons_fee").val() == '') {
        Tip.focus("coupons_fee", "请输入优惠券面值!", "right");
        return false;
    }
    if ($("#docount").val() == '') {
        Tip.focus("docount", "请输入每个用户可使用此优惠券数量!", "right");
        return false;
    }
    if ($("#min_fee").val() == '') {
        Tip.focus("min_fee", "请输入使用此优惠券的最小付款金额!", "right");
        return false;
    }
}
</script>
<?php  } else if($operation == 'display') { ?>
<div class="main">
	<div class="search">
		<form action="site.php" method="get">
		<input type="hidden" name="act" value="module" />
		<input type="hidden" name="do" value="coupons" />
		<input type="hidden" name="op" value="display" />
		<input type="hidden" name="name" value="huijiajie" />
		<table class="table table-bordered tb">
			<tbody>
				<tr>
					<th>关键字</th>
					<td>
						<input class="span6" name="keyword" id="" type="text" value="<?php  echo $_GPC['keyword'];?>">（名称/兑换码）
					</td>
					<td class="search-submit">
						<button class="btn pull-right span2"><i class="icon-search icon-large"></i> 搜索</button>
					</td>
				</tr>
			</tbody>
		</table>
		</form>
	</div>

    <ul class="nav nav-tabs">
        <li <?php  if($operation == 'display' && $coupon_type == '0') { ?>class="active"<?php  } ?>><a href="<?php  echo $this->createWebUrl('coupons', array('op' => 'display', 'coupon_type' => 0))?>">线下优惠券</a></li>
        <li <?php  if($operation == 'display' && $coupon_type == '1') { ?>class="active"<?php  } ?>><a href="<?php  echo $this->createWebUrl('coupons', array('op' => 'display', 'coupon_type' => 1))?>">派发优惠券</a></li>
        <li <?php  if($operation == 'display' && $coupon_type == '2') { ?>class="active"<?php  } ?>><a href="<?php  echo $this->createWebUrl('coupons', array('op' => 'display', 'coupon_type' => 2))?>">线上优惠券</a></li>
        <?php  if($operation == 'display' && $coupon_type == '2') { ?>
        <li class="active" style="float:right;" ><a href="<?php  echo $this->createWebUrl('excel', array('op' => 'display', 'coupon_type' => 2))?>">导出优惠码</a></li>
        <?php  } ?>
    </ul>

	<div style="padding:15px;">
		<table class="table table-hover">
			<thead class="navbar-inner">
				<tr>
					<th style="min-width:100px;">优惠券名称</th>
					<th style="min-width:100px;">优惠券缩略图</th>
                    <th style="width:100px;">面值</th>
					<th style="width:200px;">有效时间</th>
					<th style="width:100px;">每人可用次数</th>
					<th style="width:100px;">总张数</th>
                    <?php  if($operation == 'display' && $coupon_type == '1') { ?>
					<th style="width:100px;">派发张数</th>
                    <?php  } else { ?>
                    <th style="width:100px;">已领张数</th>
                    <?php  } ?>
                    <th style="width:100px;">剩余张数</th>
                    <th style="width:100px;">使用最小金额</th>
                    <?php  if(($operation == 'display' && $coupon_type == '0') || ($operation == 'display' && $coupon_type == '2')) { ?>
					<th style="width:100px;">兑换码</th>
                    <?php  } ?>
					<th style="text-align:right; min-width:80px;">操作</th>
				</tr>
			</thead>
			<tbody>
				<?php  if(is_array($list)) { foreach($list as $item) { ?>
				<tr>
					<td style="display:none"><?php  echo $item['id'];?></td>
					<td><?php  echo $item['names'];?></td>
					<td> 
						<?php  if($item['coupon_thumb'] !='') { ?>
        				<img src="./resource/attachment/<?php  echo $item['coupon_thumb'];?>" border="0px" width="100px;" />
        				<?php  } ?>
        			</td>
                    <td><?php  echo $item['coupon_fee'];?></td>
					<td><?php  echo date('Y-m-d', $item['start_time'])?> - <?php  echo date('Y-m-d', $item['end_time'])?></td>
					<td><?php  echo $item['docount'];?></td>
					<td><?php  echo $item['count'];?></td>
					<td><?php  echo $item['count']-$item['iscount']?></td>
					<td><?php  echo $item['iscount'];?></td>
					<td><?php  echo $item['min_fee'];?></td>
                    <?php  if($item['coupon_type'] == '0') { ?>
                    <td><?php  echo $item['ver_code'];?></td>
                    <?php  } else if($item['coupon_type'] == '2') { ?>
                    <td><input class="btn btn-info" type="button" value="查看兑换码" /></td>
                    <?php  } ?>
					<td style="text-align:right;">
                        <?php  if($item['coupon_type'] == '1') { ?>
                        <?php  if($item['count'] == $item['iscount']) { ?>
                        <a href="<?php  echo create_url('site/module/coupons', array('name' => 'shopping', 'id' => $item['id'], 'op' => 'pai'))?>" title="派发" class="btn btn-info">派发</a>
                        <?php  } else { ?>
                        <a href="javascript:;" title="派发" class="btn">派发</a>
                        <?php  } ?>
                        <?php  } ?>
                        <a href="<?php  echo create_url('site/module/coupons', array('name' => 'shopping','coupon_type' =>$item['coupon_type'],  'id' => $item['id'], 'op' => 'delete'))?>" onclick="return confirm('此操作不可恢复，确认删除？');return false;" title="删除" class="btn btn-danger">删除</a>
					</td>
				</tr>
				<?php  } } ?>
			</tbody>
		</table>

<div id="modal-open" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true" style=" width:600px;">
			<div class="modal-header"><h3>优惠券兑换码</h3><a style="float:right;margin-top:-25px;cursor: pointer;" class="btn" data-dismiss="modal" aria-hidden="true">关闭</a></div>
			<div class="modal-body">
				<div id="code_div">
					<ul id="couponcode">
					</ul>
				</div>
				
				<div id="module-menus"></div>
			</div>
			<div class="modal-footer" id="pagecount"></div>
		</div>

		<?php  echo $pager;?>
	</div>
</div>
<?php  } ?>
<script type="text/javascript">
    var curPage = 1; //当前页码
    var total,pageSize,totalPage //总记录数，每页显示数，总页数
 $(":input").on('click',function(event){
		var objTd = $(event.target);
		var url = "<?php  echo $this->createWebUrl('coupons',array('op' => 'code'))?>";
			var objTr = objTd.parent().parent();
			var coupon_id = $('td',objTr).first().html();
            //弹出弹窗
                 $('#modal-open').modal();
                //初始化第一页
                 getData(1,url,coupon_id);
            //分页链接查询
                 $("#pagecount span a").live('click',function(){
                var rel = $(this).attr("rel");
                     if(rel){
                         getData(rel,url,coupon_id);
                            }
                 });
		});


 //获取数据
 function getData(page,url,coupon_id){
     var el_location=$('#couponcode');
     var optli = [];
     $.ajax({
         type: 'POST',
         url: url,
         data: {'pageNum':page,'coupon_id':coupon_id},
         dataType:'json',
         beforeSend:function(){
//             $("#list ul").append("<li id='loading'>loading...</li>");//显示加载动画
         },
         success:function(data){
             $("#list ul").empty();//清空数据区
             total = data.total; //总记录数
             pageSize = data.pageSize; //每页显示条数
             curPage = page; //当前页
             totalPage = data.totalPage; //总页数
             if(data.status==1){
                 var couponcodes = data.couponcodearr;
                 if(couponcodes.length!==0){
                     $.each(couponcodes,function(index,item){
                         optli.push('<li>'+item+'</li>');
                     });
                 }
                 el_location.empty().append(optli.join(''));
             }
         },
         complete:function(){ //生成分页条
             getPageBar();
         },
         error:function(){
             alert("数据加载失败");
         }
     });
 }


    //获取分页条
    function getPageBar(){
        //页码大于最大页数
        if(curPage>totalPage) curPage=totalPage;
        //页码小于1
        if(curPage<1) curPage=1;
        pageStr = "<span>共"+total+"条</span><span>"+curPage+"/"+totalPage+"</span>";

        //如果是第一页
        if(curPage==1){
            pageStr += "<span>首页</span><span>上一页</span>";
        }else{
            pageStr += "<span><a href='javascript:void(0)' rel='1'>首页</a></span><span><a href='javascript:void(0)' rel='"+(curPage-1)+"'>上一页</a></span>";
        }

        //如果是最后页
        if(curPage>=totalPage){
            pageStr += "<span>下一页</span><span>尾页</span>";
        }else{
            pageStr += "<span><a href='javascript:void(0)' rel='"+(parseInt(curPage)+1)+"'>下一页</a></span><span><a href='javascript:void(0)' rel='"+totalPage+"'>尾页</a></span>";
        }
        $("#pagecount").html(pageStr);
    }
//    $(function(){
//        getData(1);
//        $("#pagecount span a").live('click',function(){
//            var rel = $(this).attr("rel");
//            if(rel){
//                getData(rel);
//            }
//        });
//    });
</script>
<?php  include $this->template('common/footer', TEMPLATE_INCLUDEPATH);?>